using System;
using System.Collections.Generic;
using Sedge.Core.Configuration;
using Sedge.Core.Execution;
using Sedge.Core.Execution.ReportElements;
using Sedge.Core.Globalization;
using Sedge.Core.Utils;

namespace Sedge.UI.ViewModel
{
	public class ErrorDetailsViewModel
	{
		private readonly Suite _suite;
		private readonly Options _options;

		public ErrorDetailsViewModel(Suite suite, Options options)
		{
			_suite = suite;
			_options = options;
		}

		public string[] GetSeverityLevels()
		{
			return Locale.Strings.DetailsSeverityLevels.Split(',');
		}

		public string[] GetReportTypes()
		{
			return Locale.Strings.DetailsReportTypes.Split(',');
		}

		public List<string> GetApplications(out string application)
		{
			application =
				_suite.SelectedApplication != null ? _suite.SelectedApplication.Caption : String.Empty;

			List<string> appName = new List<string>();
			foreach (ReportableObject app in _suite.Applications)
			{
				appName.Add(app.Caption);
			}

			return appName;
		}

		public bool CanShowExaple()
		{
			return !String.IsNullOrEmpty(_options.Example());
		}

		public void ShowExample()
		{
			WebHelper.ShowHtml(_options.Example());
		}

		public void StoreData(string app, int severity, int type, string[] description)
		{
			ReportTable reportTable = new ReportTable(_suite);
			reportTable.AddRow(Locale.TemplateStrings.ReportApplication, app);

			String severityStr = String.Empty;
			if (severity >= 0)
			{
				string[] levels = Locale.TemplateStrings.ReportSeverityLevels.Split(',');
				if (severity < levels.Length)
					severityStr = levels[severity];
			}
			reportTable.AddRow(Locale.TemplateStrings.ReportSeverity, severityStr);

			String typeStr = String.Empty;
			if (type >= 0)
			{
				string[] types = Locale.TemplateStrings.ReportTypes.Split(',');
				if (type < types.Length)
					typeStr = types[type];
			}
			reportTable.AddRow(Locale.TemplateStrings.ReportType, typeStr);

			reportTable.AddRow(Locale.TemplateStrings.ReportDescription, HtmlHelper.ArrayToBreakedString(description));
			_suite.TempData[TempDataKeys.ReportDetailsKey] = reportTable;
		    _suite.SelectApplication(app);
		}
	}
}